/**
 * Copyright (C) 2012 The SkyTvOS Project
 * <p/>
 * Version     Date           Author
 * ─────────────────────────────────────
 * 2012-10-24         luwei
 */

package com.coocaa.x.app.hall.main.ui.category;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.View;

import com.coocaa.x.framework.app.CoocaaApplication;
import com.coocaa.x.uipackage.generallayout.view.BaseCategoryBar.BaseCategoryData;


public class CategoryName extends View implements View.OnKeyListener {
    private Paint mPaint = new Paint();
    private int mCurrentPos = 0;
    private int mHSpace = 45;
    private int mOffsetX = 80;
    private int mOffsetY = 130;
    private int mDivideOffsetY = 10;
    private int mBeginX = -50;
    private int mRedTextSize = 45;
    private int mCommonTextSize = 60;
    private List<Category> mCategories = null;
    private Context mContext = null;

    public CategoryName(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        mPaint.setAntiAlias(true);
        init();
    }

    public CategoryName(Context context) {
        super(context);
        this.mContext = context;
        mPaint.setAntiAlias(true);
        init();
    }

    private void init() {
        mCategories = new ArrayList<Category>();
        mHSpace = CoocaaApplication.Div(mHSpace);
        mOffsetX = CoocaaApplication.Div(mOffsetX);
        mOffsetY = CoocaaApplication.Div(mOffsetY);
        mDivideOffsetY = CoocaaApplication.Div(mDivideOffsetY);
        mBeginX = CoocaaApplication.Div(mBeginX);
        mRedTextSize = CoocaaApplication.Dpi(mRedTextSize);
        mCommonTextSize = CoocaaApplication.Div(48);
        mPaint.setColor(Color.WHITE);
        mPaint.setTextSize(mCommonTextSize);
    }

    public List<Category> getDataList() {
        return mCategories;
    }

    public Category getCategoryData() {
        return mCategories.get(mCategories.size() - 1);
    }

    public Category getCategoryData(int index) {
        return mCategories.get(index);
    }

    public void addData(BaseCategoryData data) {
        if (mCategories == null) {
            this.mCategories = new ArrayList<Category>();
        }
        Category category = creatCategory(data, mCategories.size());
        mCategories.add(category);
        invalidate();
    }

    private Category creatCategory(BaseCategoryData data, int index) {
        Category category = new Category();
        category.id = data.id;
        category.mName = data.name;
        category.mWidth = (int) mPaint.measureText(data.name);
        if (index == 0) {
            if (mCategories.size() > 0) {
                category.mX = mCategories.get(0).mX;
            } else
                category.mX = mOffsetX;
        } else {
            category.mX = mCategories.get(index - 1).mX
                    + mCategories.get(index - 1).mWidth
                    + mHSpace * 2;
        }

        category.mY = mOffsetY;
        category.mTextSize = mCommonTextSize;
        //mCategories.add(index, category);
        return category;
    }

    private void refreshCategory(int index, int addOrRemove) {
        int i = index;

        if (addOrRemove == 0)//添加
        {
            for (i = index + 1; i < mCategories.size(); i++) {
                mCategories.get(i).mX = mCategories.get(i - 1).mX
                        + mCategories.get(i - 1).mWidth
                        + mHSpace * 2;
            }
        } else//删除
        {
            for (i = index; i < mCategories.size(); i++) {
                if (i == 0) {
                    mCategories.get(i).mX = mOffsetX;
                } else {
                    mCategories.get(i).mX = mCategories.get(i - 1).mX
                            + mCategories.get(i - 1).mWidth
                            + mHSpace * 2;
                }
            }
        }

    }

    public void addData(BaseCategoryData data, int index) {
        if (mCategories == null) {
            this.mCategories = new ArrayList<Category>();
        }

        if (index >= mCategories.size()) {
            addData(data);
            invalidate();
            return;
        }

        Category category = creatCategory(data, index);
        mCategories.add(index, category);
        refreshCategory(index, 0);
        invalidate();
    }

    public void removeData(int index) {
        if (index < mCategories.size()) {
            mCategories.remove(index);
            refreshCategory(index, 1);
            invalidate();
        }
    }

    public void setData(List<BaseCategoryData> categorys) {
        if (mCategories == null) {
            this.mCategories = new ArrayList<Category>();
        }

        final int size = mCategories.size();
        for (int i = size; i < categorys.size() + size; i++) {
            Category category = creatCategory(categorys.get(i - size), i);
            mCategories.add(category);
        }

        invalidateName();
    }

    private int measureText(String txt) {
        return (int) mPaint.measureText(txt);
    }

    public void setFocusPos(int pos) {
        this.mCurrentPos = pos;
        invalidate();
    }

    private void moveX(int x) {
        for (Category category : mCategories) {
            category.mX += lastmoveX;
            category.mX -= x;
        }

        lastmoveX = x;
    }

    int moveX = 0;
    int lastmoveX = 0;

    public void setMoveX(int x, int focus) {
        moveX = x;
        this.mCurrentPos = focus;
        invalidate();
    }

    public void invalidateName() {
        invalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        int i = 0;
        moveX(moveX);

        for (Category category : mCategories) {
            mPaint.setTextSize(category.mTextSize);
            if (category.mName.equals(mCategories.get(mCurrentPos).mName)) {
                Typeface font = Typeface.create(Typeface.SANS_SERIF, Typeface.BOLD);
                Typeface typeface = mPaint.setTypeface(font);
                mPaint.setColor(Color.WHITE);
                mPaint.setAlpha(255);
            } else {
                mPaint.setAlpha(80);
                mPaint.setTypeface(Typeface.DEFAULT);
            }

            canvas.drawText(category.mName, category.mX, category.mY, mPaint);
//            if (mRedPoint.get(i) != null && mRedPoint.get(i) != 0)
//            {
//                mPaint.setTextSize(mRedTextSize);
//                String txt = String.valueOf(mRedPoint.get(i));
//                int txtWidth = measureText(txt);
////                Rect rect = new Rect(category.mX + category.mWidth, SkyConstants.Div(14),
////                        category.mX + category.mWidth + txtWidth + SkyConstants.Div(30),SkyConstants.Div(60));
//                canvas.drawBitmap(mRedBitmap, category.mX + category.mWidth, SkyConstants.Div(14), null);
//                //canvas.drawText(txt, category.mX + category.mWidth + SkyConstants.Div(30) / 2 - 2, SkyConstants.Div(45), mPaint);
//            }
            i++;
        }
    }

    public void destory() {
//        if (mRedBitmap != null && !mRedBitmap.isRecycled())
//        {
//            mRedBitmap.recycle();
//            mRedBitmap = null;
//        }

        mPaint = null;
        if (mCategories != null) {
            mCategories.clear();
            mCategories = null;
        }
    }

    @Override
    public boolean onKey(View view, int i, KeyEvent keyEvent) {
        return false;
    }

    public class Category {
        public String id;
        public int mWidth;
        public int mHight;
        public String mName;
        public int mTextSize;
        public int mX;
        public int mY;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        // TODO Auto-generated method stub
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }


}
